package com.baidu.dwr;

import java.util.Collection;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;

/**
 * dwr云签到平台签到总次数，注册用户数
 * @author libs
 * 2015-11-26 14:38
 */
public class CloudPush {
	
	
    public static void statistics(final Long count, final Long exp){
    	Runnable run = new Runnable(){
            private ScriptBuffer script = new ScriptBuffer();
            public void run() {
                  //设置要调用的 js及参数
                  script.appendCall("show" , count, exp);
                  //得到所有ScriptSession
                 Collection<ScriptSession> sessions = Browser.getTargetSessions();
                  //遍历每一个ScriptSession
                  for (ScriptSession scriptSession : sessions){
                        scriptSession.addScript(script);
                 }
           }
    	};
    	//执行推送
    	if(ServerContextFactory.get() != null){//当前没有session
    		Browser. withAllSessions(run);
    	}
    }
    
    public static void showRecord(final String htmStr){
    	Runnable run = new Runnable(){
    		private ScriptBuffer script = new ScriptBuffer();
    		public void run() {
    			//设置要调用的 js及参数
    			script.appendCall("showRec" , htmStr);
    			//得到所有ScriptSession
    			Collection<ScriptSession> sessions = Browser.getTargetSessions();
    			//遍历每一个ScriptSession
    			for (ScriptSession scriptSession : sessions){
    				scriptSession.addScript(script);
    			}
    		}
    	};
    	//执行推送
    	Browser. withAllSessions(run);
    }
    
    /*public static void statistics(final Long count){
    	Browser.withAllSessions(new Runnable() {
		    public void run() {
		    	ScriptSessions.addFunctionCall("show", count);
		    }
		});
    	
    }*/

}
